/**
 * JAVACC DEMO 1.0
 * @copy right zbxsoft company All rights reserved. 
 * @Package com.zbxsoft.portal.send.method  
 */
package com.apache.info.template;

import java.util.List;

import com.apache.uct.common.ToolsUtil;

import freemarker.template.TemplateMethodModel;
import freemarker.template.TemplateModelException;

/**
 * description:  格式化Long类型时间
 * @author zbx 创建时间：2015-9-22  
 * @Copyright(c)2014:北京中百信软件技术有限公司
 */
public class UpperFirstMethod implements TemplateMethodModel {

	/**
	 * TODO 简单描述该方法的实现功能（可选）.  
	 * @see freemarker.template.TemplateMethodModel#exec(java.util.List)  
	 */
	public Object exec(List arguments) throws TemplateModelException {
		String str = String.valueOf(arguments.get(0));
		if (ToolsUtil.isNotNull(str)) {
			if (arguments.size() > 1) {
				return toLowerFirst(str);
			} else {
				return toUpperFirst(str);
			}
		}
		return "";
	}

	/**
	 * 首字母小写
	 * @param str
	 * @return
	 */
	private String toLowerFirst(String str) {
		if (ToolsUtil.isNull(str))
			return "";
		StringBuffer sb = new StringBuffer();
		sb.append(str.substring(0, 1).toLowerCase() + str.substring(1));
		return sb.toString();
	}

	/**
	 * 首字母大写
	 * @param str
	 * @return
	 */
	private String toUpperFirst(String str) {
		if (ToolsUtil.isNull(str))
			return "";
		String names[] = str.split("_");
		StringBuffer sb = new StringBuffer();
		if (names.length < 2) {
			sb.append(names[0].substring(0, 1).toUpperCase() + names[0].substring(1));
		} else {
			for (int i = 0; i < names.length; i++) {
				sb.append(names[i].substring(0, 1).toUpperCase() + names[i].substring(1));
			}
		}
		return sb.toString();
	}
}
